/*
  八分法画圆形
  绘制圆的八分之一段弧，利用对称法绘制出其余7段
*/
#include "OpenGLCase4.h"

// 方法一利用简单方程绘制
void drawCircle(int cx, int cy, int r, HDC hdc) {
	// 利用圆的方程 x２ + y２ = r２
	// x ∈(0, r / sqrt(2))

	int size = r / sqrt(2);
	for (int x = 0; x <= size; x++) {
		int y = sqrt(r * r - x * x);

		SetPixel(hdc, x + cx, y + cy, RGB(255, 255, 0));
		SetPixel(hdc, -x + cx, y + cy, RGB(255, 255, 0));

		SetPixel(hdc, x + cx, -y + cy, RGB(255, 255, 0));
		SetPixel(hdc, -x + cx, -y + cy, RGB(255, 255, 0));

		SetPixel(hdc, y + cx, x + cy, RGB(255, 255, 0));
		SetPixel(hdc, y + cx, -x + cy, RGB(255, 255, 0));

		SetPixel(hdc, -y + cx, x + cy, RGB(255, 255, 0));
		SetPixel(hdc, -y + cx, -x + cy, RGB(255, 255, 0));
	}
}

// 方法二利用极坐标方程
void drawCircle1(int cx, int cy, int r, HDC hdc) {
	// x = R * cosθ
	// y = R * sinθ
	// θ ∈ [0, π/4]
	// θi+1 = θi + △θ


	float pi = 3.14159;

	float size = pi / 4;
	for (float q = 0; q < size; q += 0.01) {
		int x = r * cos(q);
		int y = r * sin(q);

	//int size = r / sqrt(2);
	//for (int x = 0; x <= size; x++) {
	//	int y = r * sin(acos((float)x / (float)r));

		SetPixel(hdc, x + cx, y + cy, RGB(255, 255, 0));
		SetPixel(hdc, -x + cx, y + cy, RGB(255, 255, 0));

		SetPixel(hdc, x + cx, -y + cy, RGB(255, 255, 0));
		SetPixel(hdc, -x + cx, -y + cy, RGB(255, 255, 0));

		SetPixel(hdc, y + cx, x + cy, RGB(255, 255, 0));
		SetPixel(hdc, y + cx, -x + cy, RGB(255, 255, 0));

		SetPixel(hdc, -y + cx, x + cy, RGB(255, 255, 0));
		SetPixel(hdc, -y + cx, -x + cy, RGB(255, 255, 0));
	}
}


void runCase4() {

	// 获取一个可供画图的DC，我这里就直接用桌面算了
	HDC hdc = GetWindowDC(GetDesktopWindow());

	//drawCircle(700, 500, 100, hdc);
	drawCircle1(700, 500, 100, hdc);

}